package com.zl.org.controller;

import com.zl.basic.exception.BusinessException;
import com.zl.basic.util.AjaxResult;
import com.zl.basic.util.ExcelUtil;
import com.zl.basic.util.PageList;
import com.zl.org.domain.Shop;
import com.zl.org.domain.ShopAuditLog;
import com.zl.org.dto.ShopDto;
import com.zl.org.query.ShopQuery;
import com.zl.org.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;
    /**
     * 查询一个对象的数据
     * */
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id") Long id) {
        return shopService.findById(id);
    }
    /**
     * 查询所有
     * */
    @GetMapping
    public List<Shop> findAll() {
        return shopService.findAll();
    }
    /**
     * 分页查询
     * */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery) {

        return shopService.queryPage(shopQuery);
    }

    /**
     * 删除操作
     * */
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            shopService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败！！！");
        }
    }

    /**
     * 添加或者修改操作
     * */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody  Shop shop) {
        try {
            if (shop.getId()==null){
                shopService.save(shop);
            }else {
                shopService.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败！！！");
        }
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            shopService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败！！！");
        }
    }

    /**
     * 店铺入驻
     * */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {

        try {
            shopService.settlement(shop);
            return new AjaxResult();
        }catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult(false,"入驻失败"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后在试!!!");
        }
    }


    /**
     * 店铺入驻通过
     * */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog) {

        try {
            shopService.pass(shopAuditLog);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"审核失败");
        }
    }


    /**
     * 店铺入驻驳回
     * */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog) {

        try {
            shopService.reject(shopAuditLog);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"驳回失败");
        }
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            List<Shop> shops = shopService.findAll();
            ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"shop.xlsx",response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @PostMapping("/importExcel")
        public void importExcel(@RequestPart("file")MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        shops.forEach(System.out::println);
    }

    @GetMapping("/echarts")
    public List<ShopDto> getShopState(){
        return shopService.getShopState();
    }

   @GetMapping("/active/{id}")
    public String de (@PathVariable("id") Long id){
       Shop shop = shopService.findById(id);
       shop.setState(3);
       shopService.update(shop);
       return  "<a href='http://localhost:8081/#/login'>点击该链接登录</a>";
   }
}
